package io.github.lee.jel.ui.add

import android.content.Context
import android.content.Intent
import androidx.activity.viewModels
import androidx.appcompat.widget.Toolbar
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.core.ui.BaseActivity
import io.github.lee.core.ui.databinding.ToolbarSimpleCenterTitleBinding
import io.github.lee.jel.R
import io.github.lee.jel.databinding.ActivityAddServerBinding
import io.github.lee.jel.global.theme.ThemeManager

@AndroidEntryPoint
class AddServerActivity : BaseActivity<ActivityAddServerBinding, AddServerVM>() {

    companion object {
        @JvmStatic
        fun intent(context: Context): Intent = Intent(context, AddServerActivity::class.java)

        @JvmStatic
        fun start(context: Context) {
            val starter = intent(context)
            context.startActivity(starter)
        }
    }

    //===Desc:=============================================================
    override fun onCreateViewModel(): Lazy<AddServerVM> =
        viewModels()

    override fun onCreateToolbar(): Toolbar =
        ToolbarSimpleCenterTitleBinding.inflate(layoutInflater).apply {
            bg = ThemeManager.statusBarColor(mContext)
            title = getString(R.string.txt_add_jellyfin)
            titleColor = getColor(R.color.color_toolbar_title)
        }.toolbar

    override fun onCreateSuccess(): ActivityAddServerBinding =
        ActivityAddServerBinding.inflate(layoutInflater)

    //===Desc:=============================================================

    override fun onObserved() {
        super.onObserved()
        vm?.loginSuccessLiveData?.observe(this) {
            if (it) {
                setResult(RESULT_OK)
                finish()
                vm?.loginSuccessLiveData?.postValue(false)
            }
        }
    }

    override fun onSetViewData() {
        super.onSetViewData()
        statusBarColor(ThemeManager.statusBarColor(mContext), ThemeManager.isStatusBarDark())
        navigationBarColor(ThemeManager.statusBarColor(mContext))
        vb?.vm = vm

    }
}